package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import DAO.BaseDAO;
import DTO.Merchaned;
import DTO.Order;
import pageUtil.PageB;
import service.PageService;
import serviceImpl.MerchanedDAO;
import serviceImpl.OrderDAO;
import util.UtilMD5;

@WebServlet(name = "merchaned", urlPatterns = "/merchaned")
public class MerchanendServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public MerchanendServlet() {
	}

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String flag = null;
		flag = request.getParameter("flag");
		switch (flag) {
		case "findall":
			try {
				find_all(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;

		case "update":
			try {
				update(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case "update1":
			try {
				update1(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;

		case "query":
			try {
				query(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;

		case "delete":
			try {
				delete(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case "xd":
			try {
				xd(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		}
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		service(request, response);
	}

	@SuppressWarnings("unchecked")
	protected static void xd(HttpServletRequest request, HttpServletResponse response) throws Exception {
		@SuppressWarnings("rawtypes")
		List merchanedList = new ArrayList();
		String message = "";
		String list = request.getParameter("list");
		String id = request.getParameter("id");
		String listData[] = list.split(",,");
		for (int i = 0; i < listData.length - 1; i++) {
			String a[] = listData[i].split(",");
			String number = String.valueOf(Double.parseDouble(a[3]) / Double.parseDouble(a[1]));
			message = a[0] + "**" + a[1] + "**" + number + "**" + a[3];
			merchanedList.add(message);
		}

		merchanedList.add(listData[listData.length - 1].split(",")[1]);
		String str = "";
		for (int i = 0; i < merchanedList.size() - 1; i++) {
			str = str + merchanedList.get(i) + "=";
		}
		String zMoney = (String) merchanedList.get(merchanedList.size() - 1);
		// 最终数据:
		String time = UtilMD5.dateToString(new Date());
		Order order = new Order();
		BaseDAO<Order> ob = new OrderDAO();
		order.setDdsj(str);
		order.setZj(zMoney);
		order.setCjsj(time);
		order.setDdcjrid(id);
		String sql = "insert into `order`(ddsj,ddcjrid,cjsj,zj) values(?,?,?,?)";
		boolean boo = ob.add(order, sql);
		if (boo) {
			request.setAttribute("msg", "1");
			request.getRequestDispatcher("vip_merchaned_list.jsp").forward(request, response);
		} else {
			request.setAttribute("msg", "2");
			request.getRequestDispatcher("vip_merchaned_list.jsp").forward(request, response);
		}
	}

	protected static void update1(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			response.encodeRedirectURL("utf-8");
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html; charset=UTF-8");
			request.setCharacterEncoding("utf-8");
			BaseDAO<Merchaned> ed = new MerchanedDAO();
			String id = request.getParameter("id");
			String name = new String(request.getParameter("name").getBytes("ISO8859-1"), "UTF-8");
			String money = request.getParameter("money");
			String lb = request.getParameter("lb");
			Merchaned merchaned = new Merchaned();
			String sql = "update merchaned set name=?,money=?,lb=? where id= ?";
			merchaned.setId(Integer.parseInt(id));
			merchaned.setMoney(money);
			merchaned.setLb(lb);
			merchaned.setName(name);
			boolean boo = ed.update(merchaned, sql);
			if (boo) {
				request.setAttribute("msg", "修改成功");
				request.getRequestDispatcher("merchaned_list.jsp").forward(request, response);
			} else {
				request.setAttribute("msg", "修改失败");
				request.getRequestDispatcher("merchaned_list.jsp").forward(request, response);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected static void query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			response.encodeRedirectURL("utf-8");
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html; charset=UTF-8");
			request.setCharacterEncoding("utf-8");
			BaseDAO<Merchaned> ed = new MerchanedDAO();
			String id = request.getParameter("id");
			Merchaned merchaned = new Merchaned();
			merchaned = ed.findById(id);
			request.setAttribute("merchaned", merchaned);
			request.getRequestDispatcher("redact_merchaned.jsp").forward(request, response);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected static void delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			response.encodeRedirectURL("utf-8");
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html; charset=UTF-8");
			request.setCharacterEncoding("utf-8");
			String ID = request.getParameter("id");
			Merchaned merchaned = new Merchaned();
			BaseDAO<Merchaned> ed = new MerchanedDAO();
			merchaned.setId(Integer.parseInt(ID));
			if (ed.delete(merchaned)) {
				request.setAttribute("msg", "删除成功");
				request.getRequestDispatcher("merchaned_list.jsp").forward(request, response);
			} else {
				request.setAttribute("msg", "删除失败");
				request.getRequestDispatcher("merchaned_list.jsp").forward(request, response);

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected static void update(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			response.encodeRedirectURL("utf-8");
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html; charset=UTF-8");
			request.setCharacterEncoding("utf-8");
			BaseDAO<Merchaned> ed = new MerchanedDAO();
			String id = request.getParameter("id");
			Merchaned merchaned = new Merchaned();
			merchaned.setId(Integer.parseInt(id));
			merchaned = ed.findByPropert(merchaned);
			request.setAttribute("merchaned", merchaned);
			request.getRequestDispatcher("add_merchaned.jsp").forward(request, response);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected static void find_all(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sql = "select a.id,a.name,a.photo,a.money,b.lbname as lb from merchaned a,lb b where a.lb = b.lbcs limit ?,?";
		String sql2 = "select count(id) from merchaned";
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String currentPage = request.getParameter("cp");
		currentPage = currentPage == null ? "1" : currentPage;
		PageB<Merchaned> pageBean = new PageB<Merchaned>();
		pageBean.setCurrentPage(Integer.parseInt(currentPage));
		pageBean.setPageSize(5);
		PageService<Merchaned> pageservice = new PageService<Merchaned>(new MerchanedDAO());
		PageB<Merchaned> bean = pageservice.paging(pageBean, sql, sql2);
		String page = JSON.toJSONString(bean);
		PrintWriter out = response.getWriter();
		out.print(page);
		out.flush();
	}
}
